{%extends 'base.html'%}
{%block head%}
<script>


 $(function(){

   

 /* 计算价格数量函数*/
   function calculateTotal(){
     /* 小计价格*/
     var sumTotal = 0;
     var countGood = 0;
     $('.col07').each(function(){
		//获取数量
       count = $(this).prev().find('input').val();
		//获取单价
       price = $(this).prev().prev().text();
		//小计
       total01 = parseInt(count) * parseFloat(price);
       $(this).text(total01.toFixed(2));

       if($(this).siblings('.col01').children('input').prop('checked')){
         sumTotal += parseFloat(total01.toFixed(2));
         countGood += 1;
       }

     });
     /* 总计价格*/
     $('#sumtotal').text(sumTotal.toFixed(2));
     $('.countGood').text(countGood);
   };

   calculateTotal();


	 /* 勾选总开关*/
   $('#checkToggle').click(function(){
     state = $(this).prop('checked');
     $('.checkbox').prop({'checked':state});
     calculateTotal();
   });

   /* 单个勾选开关*/
   $('.checkbox').change(function(){
     checked();
     calculateTotal();
   });
   function checked(){
     if($('input[name=checked]:checked').length == $('input[name=checked]').length){
       $('#checkToggle').prop({'checked':true});
     }
     else{
       $('#checkToggle').prop({'checked':false});
     };
   };



   /* 新增*/
   $('.add').click(function(){
     txt = $(this).next();
     if (txt.val()<100){
       txt.val(parseFloat(txt.val())+1).blur();
     };
     goodid = $(this).parents('.cart_list_td').attr('id');
     count = txt.val();
     
     url = '/cart/edit'+goodid+'_'+count;
     console.log(url);
     $.get(url,function(data){
       if(data.status=1){
       }
     });
     	calculateTotal();
   });

   /* 减少*/
   $('.minus').click(function(){
     txt = $(this).prev();
     if (txt.val()>1){
       txt.val(parseFloat(txt.val())-1).blur();
     };
     goodid = $(this).parents('.cart_list_td').attr('id');
     count = txt.val();
     url = '/cart/edit'+goodid+'_'+count;
     $.get(url,function(data){
       if(data.status=1){
       }
     });
     calculateTotal();
   });

   /* 手动修改*/
   $('.num_show').blur(function(){
     count = $(this).val();
     if (count<=0){
       $(this).val(1);
     }
     else if(count>=100){
       $(this).val(99);
     }
     goodid = $(this).parents('.cart_list_td').attr('id');
     count = $(this).val();
     url = '/cart/edit'+goodid+'_'+count+'/';
     $.get(url,function(data){
     });
     calculateTotal();
   });


    $('.del').click(function(){
      del=confirm('确定要删除吗？');
      var cart_id =$(this).attr('id');
      if(del){
   			$.get('/cart/delete'+cart_id+'/',function(data){
   				if(data.ok==1){
                $('ul').remove('#'+cart_id);
                $('.total_count1').text(data.count)
                $('.countGood').text(data.count)
                
                calculateTotal();
              
              
  				}

   			});
   		}
    })



 })

 
 </script>
{%endblock head%}
{%block body%}
	<div class="total_count">全部商品<em class="total_count1 countGood"></em>件</div>
	<ul class="cart_list_th clearfix">
		<li class="col01">商品名称</li>
		<li class="col02">商品单位</li>
		<li class="col03">商品价格</li>
		<li class="col04">数量</li>
		<li class="col05">小计</li>
		<li class="col06">操作</li>
	</ul>
<form action="/order/order" method="GET">
    {%for cart in carts%}

	<ul class="cart_list_td clearfix" id="{{cart.id}}">
		<li class="col01"><input type="checkbox" name="cart_id" value="{{cart.id}}" checked="" class="checkbox"></li>
		<li class="col02"><img src="/static/{{cart.goods.gpic}}"></li>
		<li class="col03">{{cart.goods.gtitle}}<br><em>{{cart.goods.gprice}}元/{{cart.goods.gunit}}</em></li>
		<li class="col04">{{cart.goods.gunit}}</li>
		<li class="col05">{{cart.goods.gprice}}</li>
		<li class="col06">
			<div class="num_add">
				<a href="javascript:;" class="add fl">+</a>
				<input type="text" class="num_show fl" value="{{cart.count}}">
				<a href="javascript:;" class="minus fl">-</a>
			</div>
		</li>
		<li class="col07">25.80元</li>
		<li class="col08"><a href="javascript:;" id="{{cart.id}}" class="del">删除</a></li>
		<!--class="del_good-->
	</ul>
	{%endfor%}

	<ul class="settlements">
		<li class="col01"><input type="checkbox" name="" checked="" id="checkToggle"></li>
		<li class="col01"><input type="checkbox" name="" checked="" id="checkToggle"></li>
		<li class="col02">全选</li>
    <li class="col02_2"><a href="/cart/delete_all">全部删除</a></li>
		<li class="col03">合计(不含运费)：<span>¥</span><em id="sumtotal"></em><br>共计<b class="countGood"></b>件商品</li>
		<li class="col04"><input class="order" name="" type="submit" value="去结算"/></li>
	</ul>
</form>
{%endblock body%}